package org.correct.link;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

public class Preprocessor {
	private BufferedReader reader;
	private PrintWriter writer;
	private HashMap<String, Integer> map;
	
	public Preprocessor() {};
	
	public void openInputFile(String fname) {
		try{
			reader = new BufferedReader(new FileReader(fname));
		} catch (FileNotFoundException e) {
			System.err.println("Input file not found...");
			e.printStackTrace();
		}
	}
	
	public void closeInputFile() {
		try {
			reader.close();
		} catch (IOException e) {
			System.err.println("Failed to close input file...");
			e.printStackTrace();
		}
	}
	
	public void openOutputFile(String fname) {
		try{
			writer = new PrintWriter(fname);
		} catch(FileNotFoundException e) {
			System.err.println("Failed to open output file...");
			e.printStackTrace();
		}
	}
	
	public void closeOutputFile() {
		writer.close();
	}
	
	public int preprocess() {
		map = new HashMap<String, Integer>();
		String line = null;
		ArrayList<String> plist = null;
		String tstr = null;
		int index = -1;
		int i = 0;
		int count = 0;
		try{
			line = reader.readLine();
			while(line != null) {
				plist = Utility.split(line, ' ');
				if(plist != null && plist.size() > 0) {
					for(i = 0; i < plist.size(); i++) {
						tstr = plist.get(i);
						if(map.containsKey(tstr)) {
							writer.print(map.get(tstr) + " ");
						} else {
							index++;
							map.put(tstr, index);
							writer.print(index + " ");
						}
					}
					writer.println();
				}
				count++;
				if(count % 10000 == 0)
					System.out.println("Line " + count + " has been preprocessed...");
				line = reader.readLine();
			}
			writer.flush();
			System.out.println("Total pages: " + map.size());
		} catch(IOException e) {
			System.out.println("Error when reading or writing files...");
			e.printStackTrace();
		}
		
		System.out.println("Preprocessing finished...");
		return map.size();
	}
	
	public HashMap<String, Integer> getMap() {
		return map;
	}
}
